//
//  ViewController.m
//  线程间通信
//
//  Created by hyl on 16/7/15.
//  Copyright © 2016年 hyl. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //self.imageView.image=image;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //
    [self performSelectorInBackground:@selector(downloadImage) withObject:nil];
}

-(void) downloadImage{
    NSURL *url=[NSURL URLWithString:@"http://img.lanrentuku.com/img/allimg/1508/51-150R51U1240-L.jpg"];
    NSData *data=[NSData dataWithContentsOfURL:url];
    UIImage *image=[UIImage imageWithData:data];
    //回主线程的有四种方法
    //第一种：相当于self.imageView.image=image;
//    [self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:YES];
    //第二种
//    [self performSelectorOnMainThread:@selector(showImage:) withObject:image waitUntilDone:YES];
    //第三种
    [self.imageView performSelector:@selector(setImage:) onThread:[NSThread mainThread] withObject:image waitUntilDone:YES ];
    
    //第四种：只做了解
    //    NSPort;
    //    NSMessagePort;
    //    NSMachPort;
}

-(void) showImage:(UIImage *)image{
    self.imageView.image=image;
}
@end
